#!/bin/bash

function usage() {
    echo "$( basename $0 ) [-1 | -2 | -a | -h | -r | -c]"
    echo "    Run eb qemu"
    echo "    参数说明:"
    echo "        -1: 启动qemu实例1, 默认"
    echo "        -2: 启动qemu实例2"
    echo "        -r: 重新启动qemu实例"
    echo "        -c: clean qemu实例"
    echo "        -a: 启动qemu实例1,2"
    echo "        -h: 打印帮助信息"
    exit 1
}

set -e 

paramRestart=""
actClean=false
while getopts ":12ahrc" args
do
    case "$args" in
        1)
            instances=(1)
            ;;
        2)
            instances=(2)
            ;;
        a)
            instances=(1 2)
            ;;
        r)
            paramRestart="--restart"
            ;;
        c)
            actClean=true
            ;;
        h)
            usage
            ;;
        *)
            usage
            ;;
    esac
done

if [ 0 -eq ${#instances[@]} ]
then
    instances=(1)
fi

for i in "${instances[@]}"
do
    if [ true == "${actClean}" ]
    then
        echo ara-cli RunQemu --start $i --clean
        ara-cli RunQemu --start $i --clean
    else
        echo ara-cli RunQemu --start $i ${paramRestart} --target-os eblinux
        ara-cli RunQemu --start $i ${paramRestart} --target-os eblinux
    fi
done
